<template>
	<view class="wx-main">
		<view class="wx-title">点击底部按钮生成随机数</view>
		<view class="number">
			<block v-if="!isNull(resStr)">
				<view class="item" v-for="item in resList" :key="item">
					<block v-if="isNull(item.term)">
						<view class="inner" v-for="(item,index) in item" :key="item">
							<text :class="index == 6?'blue':'red'">{{item}}</text>
						</view>
					</block>
					<blcok v-else>
						<view>
							{{item}}
						</view>
					</blcok>	
				</view>
				<view v-if="!isNull(resList)" style="text-align: center;padding: 15px;">
					<text @click="copyNo()" class="copy-btn" @longpress="copyAll()">复制号码</text>
				</view>
			</block>
		</view>
		<view class="bar">
			<view class="btn" @click="createNo(1)">生成一组</view>
			<view class="btn" @click="createNo(3)">生成三组</view>
			<view class="btn" @click="createNo(5)">生成五组</view>
		</view>
	</view>
</template>

<script>
	import request from "@/util/request.js"
	import util from "@/util/util.js"
	import { onHide } from "@dcloudio/uni-app";
	import {
		ref,
		onMounted,
		reactive
	} from "vue"
	export default {
		setup() {
			onMounted(() => {

			})

			let resList = ref([])
			let resStr = ref('`')
			let copyFlag = 0
			async function createNo(g) {
				if (util.isNull(resStr.value)) return
				resStr.value = ''
				copyFlag = 0
				uni.showLoading({
					title: "计算中…"
				})
				let p = {}
				p.group = g
				p.luckyNo = []
				let data = []
				let _resStr = ''
				console.log(p)
				let res = await request.goSsq(p)
				// console.log(res)
				if (res.code == 10000) {
					data = util.isNull(res.data)?{term: '啥也没有，再试试'}:res.data
					resList.value = data
					if (util.isNull(data[0].term)) {
						for(let i in data) {
							_resStr = _resStr+'\n'+JSON.stringify(data[i])
						}
					} else {
						_resStr = data[0].term+'@'+data[0].date+'['+data[0].ball1+','+data[0].ball2+','+data[0].ball3+','+data[0].ball4+','+data[0].ball5+','+data[0].ball6+','+data[0].ball7+']'
					}
				}
				setTimeout(()=>{
					uni.hideLoading()
					resStr.value = _resStr
				},3000)
			}
			
			function copyNo() {
				// console.log(resStr.value)
				copyFlag = 1
				uni.setClipboardData({
					data: resStr.value,
					success: (res) => {
						uni.showToast({
							title: '号码复制成功'
						})
					}
				})
			}
			
			function copyAll() {
				copyFlag++
				uni.setClipboardData({
					data: resStr.value,
					success: (res) => {
						if (copyFlag > 1) {
							uni.setStorageSync('not-simple',true)
							uni.showToast({
								title: '已退出简易模式，重启小程序查看',
								icon: 'none'
							})
						}
					}
				})
			}
			
			// render util
			function isNull(s) {
				return util.isNull(s)
			}
			
			function formatAdd0(s) {
				return s*1<10?'0'+s:s
			}

			return {
				resList,resStr,createNo,copyNo,copyAll,isNull,formatAdd0
			}
		},
	}
</script>
<style lang="scss" scoped src="./com_page.scss"></style>
